#include "chooselevelscene.h"
#include<QMenuBar>
#include<QPainter>
#include<mypushbutton.h>
#include<QDebug>
#include<QTimer>
#include<QLabel>
#include<QSound>

ChooseLevelScene::ChooseLevelScene(QWidget *parent) : QMainWindow(parent)
{
    //配置选择关卡场景
    this->setFixedSize(390,640);

    //设置图标
    this->setWindowIcon(QPixmap(":/res/Coin0001.png"));
    this->setWindowTitle("唉，这这，这关卡都齐了，怎么还不选啊？");

    //创建菜单栏
    QMenuBar *bar=menuBar();
    setMenuBar(bar);

    //创建开始菜单
    QMenu *startMenu= bar->addMenu("开始");

    //创建退出项
    QAction * quitAction=startMenu->addAction("退出");
    connect(quitAction,&QAction::triggered,[=](){
        this->close();
    });

    //返回按钮音效创建
    QSound *backSound =new QSound(":/res/BackButtonSound.wav",this);
    //选关按钮音效创建
    QSound *chooseSound =new QSound(":/res/TapButtonSound.wav",this);

    //选择背景音效创建
    QSound *startBkSound =new QSound(":/res/JT02choose.wav",this);
    startBkSound->play();

    //back按钮
    MyPushButton *backbtn=new MyPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
    backbtn->setParent(this);
    backbtn->move(this->width()-backbtn->width(),this->height()-backbtn->height());
    connect(backbtn,&MyPushButton::clicked,[=](){
        //返回音效
        backSound->play();
        startBkSound->stop();



        //qDebug()<<"点击了返回按钮";
        //告诉主场景 我返回了，主场景监听返回按钮
        //延时返回
        QTimer::singleShot(500,this,[=](){
            emit this->chooseSceneBack();
        });
    });
    //创建选择关卡的按钮
    for(int i=0;i<20;i++){

        MyPushButton *menuBtn=new MyPushButton(":/res/LevelIcon.png");
        menuBtn->setParent(this);
        menuBtn->move(30+i%4*85 ,140+i/4*85);

        //监听每个按钮的点击
        connect(menuBtn,&MyPushButton::clicked,[=](){
            //播放选关音效
            chooseSound->play();

            QString str=QString("您选择的是第%1关").arg(i+1);
            qDebug()<<str;



            startBkSound->stop();
            //延时进入选择关卡场景
            QTimer::singleShot(400,this,[=](){
                //进入相应关卡
                this->hide();//隐藏选关界面

                play=new PlayScene(i+1);//生成相应关卡

                //游戏界面初始位置
                play->setGeometry(this->geometry());


                //显示游戏关卡
                play->show();

                //监听返回
                connect(play,&PlayScene::playSceneBack,[=](){
                    //返回选择界面初始位置
                    this->setGeometry(play->geometry());

                    this->show();

                    startBkSound->play();

                    delete play;
                    play=NULL;
                });

            });

        });

        QLabel * label=new QLabel;
        label->setParent(this);
        label->setFixedSize(menuBtn->width(),menuBtn->height());
        label->setText(QString::number(i+1));
        label->move(30+i%4*85 ,140+i/4*85);

        //设置对齐方式
        label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
        //鼠标穿透  51号
        label->setAttribute(Qt::WA_TransparentForMouseEvents);

    }

}

void ChooseLevelScene::paintEvent(QPaintEvent *){
    //背景
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/OtherSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

    //标题
    pix.load(":/res/Title.png");
    painter.drawPixmap( (this->width()-pix.width() )/2 ,30,pix.width(),pix.height(),pix);

}
